	require 'net/https'
	require "rexml/document"

def list_users
	resp = "";
	http = Net::HTTP.new("localhost", 3000)
	http.start do |http|
		req = Net::HTTP::Get.new("/users.xml")
		response = http.request(req)
		resp = response.body
	end
	
	doc = REXML::Document.new(resp)
	REXML::XPath.each(doc, '//users/user') do |user|
		username = REXML::XPath.match(user,'./username')
		userid = REXML::XPath.match(user,'./id')
		puts "User name : #{username[0].get_text.to_s} \t USER ID: #{userid[0].get_text.to_s}"
	end
end


def get_user(userID)
	resp = "";
	http = Net::HTTP.new("localhost", 3000)
	http.start do |http|
		req = Net::HTTP::Get.new("/users/#{userID}.xml")
		response = http.request(req)
		resp = response.body
	end
	resp

	doc = REXML::Document.new(resp)
	username = REXML::XPath.match(doc,'//user/username')
	userid = REXML::XPath.match(doc,'//user/id')
	password = REXML::XPath.match(doc,'//user/password')
	email = REXML::XPath.match(doc,'//user/email')

	puts "USER NAME : #{username[0].get_text.to_s} \t USER ID: #{userid[0].get_text.to_s}"
	puts "PASSWORD  : #{password[0].get_text.to_s} \t EMAIL  : #{email[0].get_text.to_s}"
end

def create_user(userName, email, password)

	data = "<?xml version='1.0' encoding='UTF-8'?>
		<user>
			<username>#{userName}</username>
			<password>#{password}</password>
			<email>#{email}</email>
		</user>"

	response = "";
	http = Net::HTTP.new("localhost", 3000)
	http.start do |http|
		req = Net::HTTP::Post.new("/users.xml", 
		{'Connection' => 'close', 'Accept' => '*/*', 
		 'Content-Type'=>'application/xml'})
		req.body = data
		response = http.request(req).body	
	end

	doc = REXML::Document.new(response)
	userid = REXML::XPath.match(doc,'//user/id')
	id = userid[0].get_text.to_s
end

def delete_user(userID)
	http = Net::HTTP.new("localhost", 3000)
	http.start do |http|
		req = Net::HTTP::Delete.new("/users/#{userID}.xml")
		response = http.request(req)
		resp = response.body
	end
end

userID = 1

puts "-----------------------------------------------"
puts "This client uses 'net/https' gem to access user"
puts "management RESTful web service"
puts "This application will list all the users that  "
puts "are currently registered with system, then will"
puts "demonstre user create and delete operations"
puts "-----------------------------------------------"
puts "CURRENT LIST OF USERS"
puts "-----------------------------------------------"
list_users 
puts "-----------------------------------------------"
puts "CREATING NEW USER"
puts "-----------------------------------------------"

begin
	puts "please enter user name (5 or more symbols):"
	username = gets.chomp
end until (username.length > 4)

begin
	puts "please enter password (5 or more symbols):"
	password = gets.chomp
end until (password.length > 4)

puts "please enter email:"
email = gets.chomp
userID = create_user(username, email,password)
puts "user with id #{userID} have been created"
puts "-----------------------------------------------"
puts "CURRENT LIST OF USERS"
puts "-----------------------------------------------"
list_users 
puts "-----------------------------------------------"
puts "INFO ABOUT USER WITH ID #{userID}"
puts "-----------------------------------------------"
get_user(userID)
puts "-----------------------------------------------"
puts "DELETING USER WITH ID #{userID}"
puts "-----------------------------------------------"
delete_user(userID)
puts "user with id #{userID} have been deleted"
puts "-----------------------------------------------"
puts "CURRENT LIST OF USERS"
puts "-----------------------------------------------"
list_users 
puts "-----------------------------------------------"
